package core

import "sync"

type PluginManager struct {
	plugins map[string]Plugin
	mu      sync.Mutex
}

func NewPluginManager() *PluginManager {
	return &PluginManager{plugins: make(map[string]Plugin)}
}

func (pm *PluginManager) Register(p Plugin) {
	pm.mu.Lock()
	defer pm.mu.Unlock()
	pm.plugins[p.Name()] = p
}

func (pm *PluginManager) StartAll() {
	for _, p := range pm.plugins {
		_ = p.Start()
	}
}

func (pm *PluginManager) StopAll() {
	for _, p := range pm.plugins {
		_ = p.Stop()
	}
}

func (pm *PluginManager) Dispatch(name string, data []byte) {
	if p, ok := pm.plugins[name]; ok {
		p.Recv(data)
	}
}
